#include "ZenSprite.h"

CZenSprite::CZenSprite()
{
	m_pSprite  = 0;
	m_pTexture = 0;

	m_bInitialized = FALSE;

	m_RotCenter.x = 0.0f;
	m_RotCenter.y = 0.0f;

	m_Translation.x = 0.0f;
	m_Translation.y = 0.0f;

	m_Scaling.x = 0.0f;
	m_Scaling.y = 0.0f;

	m_ModulateColor = D3DCOLOR_XRGB(255, 255, 255);

	m_Rotation = 0.0f;
}

CZenSprite::~CZenSprite()
{
	if (m_pSprite) 
		m_pSprite->Release();

	if (m_pTexture)
		m_pTexture->Release();
}

HRESULT CZenSprite::Initialize(char * TextPathName)
{
	HRESULT res = 0;

	res = D3DXCreateTextureFromFile(zen.m_pDevice, TextPathName, &m_pTexture);
	if (FAILED(res)) 
	{
		zen.SetError("Kan geen texture laden voor sprite, naam:");
		zen.SetError(TextPathName);
		return E_FAIL;
	}

	res = D3DXCreateSprite(zen.m_pDevice, &m_pSprite);
	if (FAILED(res)) 
	{
		zen.SetError("Kan geen Sprite maken!");
		return E_FAIL;
	}

	m_bInitialized = TRUE;

	return S_OK;
}

HRESULT CZenSprite::Initialize(LPDIRECT3DTEXTURE8 pTexture)
{
	HRESULT res = 0;

	if (!pTexture)
		return E_FAIL;

	m_pTexture = pTexture;
	pTexture->AddRef();

	res = D3DXCreateSprite(zen.m_pDevice, &m_pSprite);
	if (FAILED(res)) 
	{
		zen.SetError("Kan geen Sprite maken!");
		return E_FAIL;
	}

	m_bInitialized = TRUE;

	return S_OK;
}

HRESULT CZenSprite::Render()
{
	HRESULT res = 0;

	if (!m_bInitialized)
		return E_FAIL;

	m_pSprite->Begin();
	{
		res = m_pSprite->Draw(m_pTexture, NULL, &m_Scaling, &m_RotCenter,
							  m_Rotation, &m_Translation, m_ModulateColor);
		if (FAILED(res))
			zen.SetError("Kan CZenSprite niet renderen.");
	}
	m_pSprite->End();

	return S_OK;
}

int CZenSprite::GetSize()
{
	return(sizeof(*this));
}
